<?php
namespace App\Admin\Controllers;

use App\Admin\Filters\AdminPermissionFilter;
use App\Admin\Models\AdminPermission;
use App\Admin\Requests\AdminPermissionRequest;
use App\Admin\Resources\AdminPermissionResource;

class AdminPermissionController extends Controller
{
    public function store(
        AdminPermissionRequest $request, 
        AdminPermission $adminPermission
    ){
        $inputs = $request->validated();
        $adminPermission = $adminPermission->add($inputs);

        return $this->created(
            AdminPermissionResource::make($adminPermission)
        );
    }

    public function create()
    {
        return $this->ok();
    }

    public function update(
        AdminPermissionRequest $request, 
        AdminPermission $adminPermission
    ){
        $inputs = $request->validated();
        $adminPermission->update($inputs);

        return $this->created(
            AdminPermissionResource::make($adminPermission)
        );
    }

    public function edit(AdminPermission $adminPermission)
    {
        return $this->ok(
            AdminPermissionResource::make($adminPermission)
        );
    }
    
    public function destroy(
        AdminPermission $adminPermission
    ){
        $adminPermission->delete();
        return $this->noContent();
    }

    public function index(
        AdminPermissionFilter $filter,
    ){
        $adminPermissions = AdminPermission::withTrashed(isRestore())
            ->filter($filter)
            ->select();
        $adminPermissions = request()->get('all') ? $adminPermissions->get() : $adminPermissions->paginate();
        return $this->ok(
            AdminPermissionResource::collection($adminPermissions)
        );
    }

    public function show(
        Int $id
    ){
        $adminPermission = AdminPermission::withTrashed(isRestore())
            ->find($id);
        return $this->ok(
            AdminPermissionResource::make($adminPermission)
        );
    }
}
